import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class LibraryBooksState extends ChangeNotifier {
  // 随机单词
  var current = WordPair.random();

  void getNext() {
    current = WordPair.random();
    notifyListeners();
  }

  // 收藏
  var favorites = <WordPair>[];

  void toggleFavorite(WordPair pair) {
    if (favorites.contains(current)) {
      favorites.remove(current);
    } else {
      favorites.add(current);
    }
    notifyListeners();
  }
}

class BigCard extends StatelessWidget {
  const BigCard({super.key, required this.randomWords});

  final WordPair randomWords;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final style = theme.textTheme.displayMedium!.copyWith(
      color: theme.colorScheme.onPrimary,
    );

    return Card(
      color: theme.colorScheme.primary,
      elevation: 4,
      child: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Text(
          randomWords.asLowerCase.toString(),
          style: style,
          semanticsLabel: "${randomWords.first} ${randomWords.second}",
        ),
      ),
    );
  }
}

class LibraryBooksWidget extends StatelessWidget {
  const LibraryBooksWidget({super.key});
  @override
  Widget build(BuildContext context) {
    var testState = context.watch<LibraryBooksState>();
    var randomWord = testState.current;

    IconData icon;
    if (testState.favorites.contains(randomWord)) {
      icon = Icons.favorite;
    } else {
      icon = Icons.favorite_border;
    }
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            BigCard(randomWords: randomWord),
            SizedBox(height: 10),
            Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                ElevatedButton.icon(
                  onPressed: () => testState.toggleFavorite(randomWord),
                  icon: Icon(icon),
                  label: Text('喜欢'),
                ),
                SizedBox(width: 10),
                ElevatedButton(
                  onPressed: () => testState.getNext(),
                  child: Text('下一个'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
